/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package entity;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;

public class ContattoDAO {
	public static Contatto loadContattoByORMID(int ID) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return loadContattoByORMID(session, ID);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto getContattoByORMID(int ID) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return getContattoByORMID(session, ID);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByORMID(int ID, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return loadContattoByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto getContattoByORMID(int ID, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return getContattoByORMID(session, ID, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByORMID(PersistentSession session, int ID) throws PersistentException {
		try {
			return (Contatto) session.load(entity.Contatto.class, new Integer(ID));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto getContattoByORMID(PersistentSession session, int ID) throws PersistentException {
		try {
			return (Contatto) session.get(entity.Contatto.class, new Integer(ID));
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Contatto) session.load(entity.Contatto.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto getContattoByORMID(PersistentSession session, int ID, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			return (Contatto) session.get(entity.Contatto.class, new Integer(ID), lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto[] listContattoByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return listContattoByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto[] listContattoByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return listContattoByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto[] listContattoByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From entity.Contatto as Contatto");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Contatto[]) list.toArray(new Contatto[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto[] listContattoByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From entity.Contatto as Contatto");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Contatto[]) list.toArray(new Contatto[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return loadContattoByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return loadContattoByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Contatto[] contattos = listContattoByQuery(session, condition, orderBy);
		if (contattos != null && contattos.length > 0)
			return contattos[0];
		else
			return null;
	}
	
	public static Contatto loadContattoByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Contatto[] contattos = listContattoByQuery(session, condition, orderBy, lockMode);
		if (contattos != null && contattos.length > 0)
			return contattos[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateContattoByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return iterateContattoByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateContattoByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = entity.CaranonnaPersistentManager.instance().getSession();
			return iterateContattoByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateContattoByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From entity.Contatto as Contatto");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateContattoByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From entity.Contatto as Contatto");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto createContatto() {
		return new entity.Contatto();
	}
	
	public static boolean save(entity.Contatto contatto) throws PersistentException {
		try {
			entity.CaranonnaPersistentManager.instance().saveObject(contatto);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean delete(entity.Contatto contatto) throws PersistentException {
		try {
			entity.CaranonnaPersistentManager.instance().deleteObject(contatto);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean deleteAndDissociate(entity.Contatto contatto)throws PersistentException {
		try {
			if(contatto.getUtente() != null) {
				contatto.getUtente().contatto.remove(contatto);
			}
			
			return delete(contatto);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean deleteAndDissociate(entity.Contatto contatto, org.orm.PersistentSession session)throws PersistentException {
		try {
			if(contatto.getUtente() != null) {
				contatto.getUtente().contatto.remove(contatto);
			}
			
			try {
				session.delete(contatto);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean refresh(entity.Contatto contatto) throws PersistentException {
		try {
			entity.CaranonnaPersistentManager.instance().getSession().refresh(contatto);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static boolean evict(entity.Contatto contatto) throws PersistentException {
		try {
			entity.CaranonnaPersistentManager.instance().getSession().evict(contatto);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static Contatto loadContattoByCriteria(ContattoCriteria contattoCriteria) {
		Contatto[] contattos = listContattoByCriteria(contattoCriteria);
		if(contattos == null || contattos.length == 0) {
			return null;
		}
		return contattos[0];
	}
	
	public static Contatto[] listContattoByCriteria(ContattoCriteria contattoCriteria) {
		return contattoCriteria.listContatto();
	}
}
